'use strict';

const Controller = require('egg').Controller;

class EnterpriseController extends Controller {
  // 获取企业列表
  async getEnterpriseList() {
    const { ctx } = this;
    const { page = 1, pageSize = 10, keyword = '', status = '', name = '' } = ctx.query;
    
    try {
      const result = await ctx.service.job1.enterprise.getEnterpriseList({ 
        page, 
        pageSize, 
        keyword: keyword || name, // 支持 name 和 keyword 参数
        status 
      });
      ctx.body = {
        code: 200,
        message: '获取成功',
        data: result.enterprises || [],
        pagination: {
          current: result.pagination.page,
          pageSize: result.pagination.pageSize,
          total: result.pagination.total,
          pages: Math.ceil(result.pagination.total / result.pagination.pageSize)
        }
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: error.message || '获取企业列表失败',
        data: null
      };
    }
  }

  // 删除企业
  async deleteEnterprise() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    
    try {
      await ctx.service.job1.enterprise.deleteEnterprise(enterpriseId);
      ctx.body = {
        code: 200,
        message: '删除成功',
        data: null
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: error.message || '删除企业失败',
        data: null
      };
    }
  }

  // 创建企业
  async createEnterprise() {
    const { ctx } = this;
    
    // 从JWT获取当前用户ID
    const userId = ctx.user?.id;
    if (!userId) {
      ctx.body = {
        code: 401,
        data: null,
        msg: '未登录'
      };
      return;
    }

    try {
      // 处理文件上传
      const files = ctx.request.files;
      const logoFile = files?.logo;
      const imageFile = files?.image;
      
      const enterpriseData = {
        enterpriseName: ctx.request.body.enterpriseName,
        description: ctx.request.body.description,
        benefits: ctx.request.body.benefits || '',
        status: 'active'
      };

      // 调用service创建企业（传入文件和用户ID）
      const result = await ctx.service.job1.enterprise.createEnterprise(enterpriseData, logoFile, imageFile, userId);
      
      ctx.body = {
        code: 201,
        message: '创建成功',
        data: result
      };
    } catch (error) {
      console.error('创建企业失败:', error);
      ctx.body = {
        code: 500,
        message: error.message || '创建企业失败',
        data: null
      };
    }
  }

  // 获取企业信息
  async getEnterpriseInfo() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    
    try {
      const enterprise = await ctx.service.job1.enterprise.getEnterpriseInfo(enterpriseId);
      ctx.body = {
        code: 200,
        message: '获取成功',
        data: enterprise
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: error.message || '获取企业信息失败',
        data: null
      };
    }
  }

  // 更新企业信息
  async updateEnterpriseInfo() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    const updateData = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.enterprise.updateEnterpriseInfo(enterpriseId, updateData);
      ctx.body = {
        code: 200,
        message: '更新成功',
        data: result
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        message: error.message || '更新企业信息失败',
        data: null
      };
    }
  }

  // 获取企业统计数据
  async getEnterpriseStatistics() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    
    try {
      const statistics = await ctx.service.job1.enterprise.getEnterpriseStatistics(enterpriseId);
      ctx.body = {
        code: 0,
        data: statistics,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '获取统计数据失败'
      };
    }
  }

  // 获取企业评价列表
  async getEnterpriseReviews() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    const { page = 1, pageSize = 10 } = ctx.query;
    
    try {
      const result = await ctx.service.job1.enterprise.getEnterpriseReviews(enterpriseId, { page, pageSize });
      ctx.body = {
        code: 0,
        data: result,
        msg: '获取成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '获取评价列表失败'
      };
    }
  }

  // 回复企业评价
  async replyEnterpriseReview() {
    const { ctx } = this;
    const { reviewId } = ctx.params;
    const { replyContent } = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.enterprise.replyEnterpriseReview(reviewId, replyContent);
      ctx.body = {
        code: 0,
        data: result,
        msg: '回复成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '回复失败'
      };
    }
  }

  // 处理企业投诉
  async processEnterpriseComplaint() {
    const { ctx } = this;
    const { complaintId } = ctx.params;
    const processData = ctx.request.body;
    
    try {
      const result = await ctx.service.job1.enterprise.processEnterpriseComplaint(complaintId, processData);
      ctx.body = {
        code: 0,
        data: result,
        msg: '处理成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '处理投诉失败'
      };
    }
  }

  // 上传企业图片
  async uploadEnterpriseImage() {
    const { ctx } = this;
    const { enterpriseId } = ctx.params;
    
    try {
      const result = await ctx.service.job1.enterprise.uploadEnterpriseImage(enterpriseId, ctx.request.files);
      ctx.body = {
        code: 0,
        data: result,
        msg: '上传成功'
      };
    } catch (error) {
      ctx.body = {
        code: 500,
        data: null,
        msg: error.message || '上传失败'
      };
    }
  }
}

module.exports = EnterpriseController;
